[mypyc] Support deleting attributes in __setattr__ wrapper #19997
+333
−8
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
__setattr__wrapper that mypyc generates needs to handle deleting attributes as well becausedelstatements go through the sametp_setattropointer but with the value argument set toNULL.The wrapper calls
__delattr__in this case if it's overridden in the native class (or its parent). Handling of dynamic attributes is different without__dict__which makes a custom__delattr__required if the dynamic attributes are stored in a custom dictionary.If
__delattr__is not overridden it calls the implementation ofobject.__delattr__which results inAttributeErrorbecause there's no__dict__.If it's defined without
__setattr__, mypyc reports an error. It's possible to support just__delattr__but since it shares a slot with__setattr__, the wrapper generation would be more complicated. It seems like an unlikely use case to only need__delattr__so I think it makes sense to leave it for later.